<?php

define('ACC',true) || exit('Access Denied');

require('../includes/init.php');

if(!isset($_SESSION['auth_list'])){
	$smarty->display('./admin/login.html');
}
$arr = explode(",", $_SESSION['auth_list']);
if(in_array('role_manage', $arr) == 0){
	$smarty->assign('message', '您没有权限进行此操作！');
	$smarty->assign('title', '系统提示');
	$smarty->assign('url', 'javascript:history.go(-1);');
	$smarty->assign('link', '返回');
	$smarty->display('./admin/error.html');
	exit;
}

$role = new RoleModel();

if($_GET['act'] == 'list'){
	
	$smarty->assign('role_list', $role->get_role_list());
	$smarty->assign('title', '角色管理');
	$smarty->assign('url', 'role.php?act=add');
	$smarty->assign('link', '添加角色');
	
	$smarty->display('./admin/role_list.html');
	
}else if($_GET['act'] == 'add'){
	
	require('./includes/priv_action.php');
	$priv_arr = $role->get_action_list();
	
	$smarty->assign('lang', $_LANG);
	$smarty->assign('priv_arr', $priv_arr);
	$smarty->assign('title', '添加角色');
	$smarty->assign('url', 'role.php?act=list');
	$smarty->assign('link', '角色列表');
	$smarty->assign('action', 'insert');
	
	$smarty->display('./admin/role_info.html');
	
}else if($_GET['act'] == 'insert'){
	
	if(isset($_POST['username'])){
		$username = trim($_POST['username']);
	}else{
		$username = '';
	}
	
	if(isset($_POST['action_code'])){
		$act_list = @join(',', $_POST['action_code']);
	}else{
		$act_list = '';
	}
	
	if(isset($_POST['role_desc'])){
		$role_desc = trim($_POST['role_desc']);
	}else{
		$role_desc = '';
	}
	
	if($username == '' || $act_list == ''){
		$smarty->assign('message', '您没有填写角色名或者没有选择角色');
		$smarty->assign('title', '系统提示');
		$smarty->assign('url', 'javascript:history.go(-1);');
		$smarty->assign('link', '返回');
		$smarty->display('./admin/error.html');
		exit;
	}
	
	$arr = $role->insert($act_list,$username);
	$username = $arr[0];
	$role_id = $arr[1];
	
	if($username != '' && $role_id != 0){
		$desc = '添加'.$username.'成功';
		$link = '角色列表';
		$url = 'role.php?act=list';
		admin_msg($smarty, $desc, $url, $link);
		exit;
	}else{
		$smarty->assign('message', '添加角色失败');
		$smarty->assign('title', '系统提示');
		$smarty->assign('url', 'javascript:history.go(-1);');
		$smarty->assign('link', '返回');
		$smarty->display('./admin/error.html');
		exit;
	}
	
}else if($_GET['act'] == 'delete'){
	
	$role_id = intval($_GET['id']);
	if($role->delete($role_id)){
		echo "1"; return;
	}else{//*********************************************
		echo "0"; return;
	}

}else if($_GET['act'] == 'edit'){
	require('./includes/priv_action.php');
	
	$priv_arr = $role->get_action_list();

	$user_role = $role->get_role_all($_GET['id']);
	$priv_str = ",".$user_role['auth_list'];
	foreach ($priv_arr as $key => $val){
		foreach($val['priv'] as $k => $v){
		
			if(strpos($priv_str, $v['action_code'])){
				
				$priv_arr[$key]['priv'][$k]['cando'] = 1;
			}
		}
	}
	$smarty->assign('lang', $_LANG);
	$smarty->assign('priv_arr', $priv_arr);
	$smarty->assign('user_id', $user_role['role_id']);
	$smarty->assign('user', $user_role);
	$smarty->assign('title', '角色管理');
	$smarty->assign('url', 'role.php?act=list');
	$smarty->assign('link', '角色列表');
	$smarty->assign('action', 'update');
	
	$smarty->display('../templates/admin/role_info.html');
	
}else if($_GET['act'] == 'update'){
	
	$data = array();
	
	if(isset($_POST['username'])){
		$data['role_name'] = trim($_POST['username']);
	}else{
		$data['role_name'] = '';
	}
	
	if(isset($_POST['action_code'])){
		$data['auth_list'] = @join(',', $_POST['action_code']);
	}else{
		$data['auth_list'] = '';
	}
	
	if(isset($_POST['role_desc'])){
		$data['role_desc'] = trim($_POST['role_desc']);
	}else{
		$data['role_desc'] = '';
	}
	
	if(isset($_GET['user_id'])){
		$role_id = intval($_GET['user_id']);
	}else{
		$role_id = 0;
	}
	
	if($data['role_name'] == '' || $data['auth_list'] == '' || $data['role_desc'] == '' || $role_id == 0){
		$smarty->assign('message', '信息填写有误！');
		$smarty->assign('title', '系统提示');
		$smarty->assign('url', 'javascript:history.go(-1);');
		$smarty->assign('link', '返回');
		$smarty->display('./admin/error.html');
		exit;
	}else if($role->update($data, $role_id)){
		$desc = '编辑'.$data['role_name'].'操作成功';
		$url = 'role.php?act=list';
		$link = '角色列表';
		admin_msg($smarty, $desc, $url, $link);
		exit;
	}else{
		$smarty->assign('message', '修改角色失败');
		$smarty->assign('title', '系统提示');
		$smarty->assign('url', 'javascript:history.go(-1);');
		$smarty->assign('link', '返回');
		$smarty->display('./admin/error.html');
		exit;
	}
	
}










?>